package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.chat.FriendFeedMessage;
import com.mask.im.business.model.entity.chat.Friendships;
import com.mask.im.business.model.entity.UserInfo;
import com.mask.im.business.mapper.IMFriendDynamicMapper;
import com.mask.im.business.mapper.FriendFeedMessageMapper;
import com.mask.im.business.mapper.FriendshipsMapper;
import com.mask.im.business.mapper.UserInfoMapper;
import com.mask.im.business.model.dto.FriendFeedMessageDTO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.entity.chat.IMFriendDynamicDO;
import com.mask.im.business.service.FriendFeedMessageService;
import com.mask.im.business.common.util.BeanCopy;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.mask.im.business.common.constant.CommonConstant.*;


@Service
public class FriendFeedMessageServiceImpl extends ServiceImpl<FriendFeedMessageMapper, FriendFeedMessage> implements FriendFeedMessageService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private IMFriendDynamicMapper IMFriendDynamicMapper;

	@Autowired
	private FriendshipsMapper friendshipsMapper;

	@Override
	public void saveFriendFeedMessage(Integer type, String content, Integer friendFeedId, Integer userId, Integer replyUserId) {
		FriendFeedMessage feedMessage = FriendFeedMessage.builder()
				.content(content)
				.type(type)
				.friendFeedId(friendFeedId)
				.replyUserId(replyUserId)
				.isReply(replyUserId == null ? ZERO : ONE)
				.isRead(0)
				.userInfoId(userId)
				.build();
		this.baseMapper.insert(feedMessage);
	}

	@Override
	public RestResponse<List<FriendFeedMessageDTO>> friendFeedMessageList() {
		Integer userInfoId = MaskIMSecurityUtils.getUserId();

		List<Integer> integerList = IMFriendDynamicMapper.selectList(new LambdaQueryWrapper<IMFriendDynamicDO>()
				.select(IMFriendDynamicDO::getId)
				.eq(IMFriendDynamicDO::getUserId, userInfoId)).stream()
				.map(IMFriendDynamicDO::getId).collect(Collectors.toList());

		List<FriendFeedMessage> friendFeedMessages = this.baseMapper.selectList(new LambdaQueryWrapper<FriendFeedMessage>()
				.in(!integerList.isEmpty(), FriendFeedMessage::getFriendFeedId, integerList)
				.ne(FriendFeedMessage::getUserInfoId, userInfoId)
				.or()
				.eq(FriendFeedMessage::getReplyUserId, userInfoId)
				.orderByDesc(FriendFeedMessage::getCreateTime));
		List<FriendFeedMessageDTO> feedMessageDTOS = friendFeedMessages.stream().map(item -> {
			FriendFeedMessageDTO friendFeedMessageDTO = BeanCopy.singleCopy(item, FriendFeedMessageDTO.class);
			UserInfo userInfo = userInfoMapper.selectById(item.getUserInfoId());
			friendFeedMessageDTO.setUserAvatar(userInfo.getAvatar());
			if (!userInfoId.equals(item.getUserInfoId())) {
				Friendships friendships = friendshipsMapper.selectOne(new LambdaQueryWrapper<Friendships>()
						.eq(Friendships::getOneUserId, userInfoId)
						.eq(Friendships::getTwoUserId, item.getUserInfoId()));
				friendFeedMessageDTO.setRemark(friendships.getRemark());
			}
			if (item.getIsReply().equals(ONE)) {
				UserInfo selectById = userInfoMapper.selectById(item.getReplyUserId());
				friendFeedMessageDTO.setReplyUserName(selectById.getNickName());
			}
			return friendFeedMessageDTO;
		}).collect(Collectors.toList());
		return RestResponse.ok(feedMessageDTOS);
	}

	@Override
	public RestResponse<Integer> friendFeedMessageCount() {
		Integer userInfoId = MaskIMSecurityUtils.getUserId();
		List<Integer> integerList = IMFriendDynamicMapper.selectList(new LambdaQueryWrapper<IMFriendDynamicDO>()
				.select(IMFriendDynamicDO::getId)
				.eq(IMFriendDynamicDO::getUserId, userInfoId)).stream()
				.map(IMFriendDynamicDO::getId).collect(Collectors.toList());

		Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<FriendFeedMessage>()
				.in(!integerList.isEmpty(), FriendFeedMessage::getFriendFeedId, integerList)
				.ne(FriendFeedMessage::getUserInfoId, userInfoId)
				.eq(FriendFeedMessage::getIsRead,ZERO)
				.or()
				.eq(FriendFeedMessage::getReplyUserId, userInfoId)
				.eq(FriendFeedMessage::getIsRead,ZERO)
				.orderByDesc(FriendFeedMessage::getCreateTime));
		return RestResponse.ok(count);
	}

	@Override
	public RestResponse<String> friendFeedMessageRead() {
		Integer userInfoId = MaskIMSecurityUtils.getUserId();

		List<Integer> integerList = IMFriendDynamicMapper.selectList(new LambdaQueryWrapper<IMFriendDynamicDO>()
				.select(IMFriendDynamicDO::getId)
				.eq(IMFriendDynamicDO::getUserId, userInfoId)).stream()
				.map(IMFriendDynamicDO::getId).collect(Collectors.toList());

		this.baseMapper.update(null, new LambdaUpdateWrapper<FriendFeedMessage>()
				.set(FriendFeedMessage::getIsRead, ONE)
				.in(!integerList.isEmpty(), FriendFeedMessage::getFriendFeedId, integerList)
				.ne(FriendFeedMessage::getUserInfoId, userInfoId)
				.or()
				.eq(FriendFeedMessage::getReplyUserId, userInfoId)
				.orderByDesc(FriendFeedMessage::getCreateTime));

		return RestResponse.ok();
	}
}

